package com.zh.web;

import com.github.pagehelper.PageInfo;
import com.zh.po.Apply;
import com.zh.po.User;
import com.zh.service.ApplyService;
import com.zh.service.UserService;
import com.zh.web.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * @description: 预算申请
 */
@RestController
@RequestMapping("/apply")
@CrossOrigin
public class ApplyController {
    @Autowired
    private ApplyService applyService;
    @Autowired
    private UserService userService;
    /**
     * @description:  根据用户id分页查询预算申请
     * @param: pageNum  页码
     * @param: pageSize 每页显示个数
     * @param: userId   用户编号
     * @return: com.zh.web.vo.ResponseVO<com.github.pagehelper.PageInfo<com.zh.po.Apply>>
     */
    @GetMapping("/page/{pageNum}/{pageSize}/{userId}")
    public ResponseVO<PageInfo<Apply>> page(@PathVariable Integer pageNum,@PathVariable Integer pageSize,@PathVariable Integer userId){
        PageInfo<Apply> applyPageInfo = applyService.findAll(pageNum, pageSize, userId);
        return new ResponseVO<>(applyPageInfo,"查询成功",2000);
    }

    /**
     * @description:    管理员分页查询所有预算申请
     * @param: pageNum  页码
     * @param: pageSize 每页显示个数
     * @return: com.zh.web.vo.ResponseVO<com.github.pagehelper.PageInfo<com.zh.po.Apply>>
     */
    @GetMapping("/page/{pageNum}/{pageSize}")
    public ResponseVO<PageInfo<Apply>> page(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        PageInfo<Apply> applyPageInfo = applyService.findAll(pageNum, pageSize);
        return new ResponseVO<>(applyPageInfo,"查询成功",2000);
    }

    /**
     * @description:  管理员通过预算申请
     * @param: apply   预算申请对象
     * @return: com.zh.web.vo.ResponseVO
     */
    @PutMapping("/access")
    public ResponseVO accessApply(@RequestBody Apply apply){
        if (apply==null){
            return new ResponseVO<>(null,"修改失败",4000);
        }
        int row = applyService.accessApply(apply);
        if (row >0 ){
            Integer applyUserId = apply.getApplyUser();
            User userByUserId = userService.getUserByUserId(applyUserId);
            if (userByUserId==null){
                return new ResponseVO<>(null,"修改失败",4000);
            }
            Double userMoney = userByUserId.getUserMoney();// 现有金额
            Double userBudget = userByUserId.getUserBudget();// 贷款总金额
            Double applyMoney = apply.getApplyMoney();// 贷款金额
            userByUserId.setUserBudget(userBudget+applyMoney);// 设置总贷款金额
            userByUserId.setUserMoney(userMoney+applyMoney);// 设置现有金额
            userService.updateUser(userByUserId);
            return new ResponseVO<>(row,"修改成功",2000);
        }
        return new ResponseVO<>(null,"修改失败",4000);
    }

    /**
     * @description:  管理员驳回预算申请
     * @param: apply   预算申请
     * @return: com.zh.web.vo.ResponseVO
     */
    @PutMapping("/back")
    public ResponseVO backApply(@RequestBody Apply apply){
        if (apply==null){
            return new ResponseVO<>(null,"修改失败",4000);
        }
        int row = applyService.backApply(apply);
        if (row >0 ){
            return new ResponseVO<>(row,"修改成功",2000);
        }
        return new ResponseVO<>(null,"修改失败",4000);
    }

    /**
     * @description: 用户撤回预算申请
     * @param: apply  预算申请
     * @return: com.zh.web.vo.ResponseVO
     */
    @PutMapping("/withdraw")
    public ResponseVO withdrawApply(@RequestBody Apply apply){
        if (apply==null){
            return new ResponseVO<>(null,"修改失败",4000);
        }

        int row = applyService.withdrawApply(apply);
        if (row >0 ){
            return new ResponseVO<>(row,"修改成功",2000);
        }
        return new ResponseVO<>(null,"修改失败",4000);
    }

    /**
     * @description: 用户重新申请预算申请
     * @param: apply  预算申请
     * @return: com.zh.web.vo.ResponseVO
     */
    @PutMapping("/reapply")
    public ResponseVO reapplyApply(@RequestBody Apply apply){
        if (apply==null){
            return new ResponseVO<>(null,"修改失败",4000);
        }

        int row = applyService.reapplyApply(apply);
        if (row >0 ){
            return new ResponseVO<>(row,"修改成功",2000);
        }
        return new ResponseVO<>(null,"修改失败",4000);
    }

    /**
     * @description: 用户新增预算申请
     * @param: apply  预算申请
     * @return: com.zh.web.vo.ResponseVO
     */
    @PostMapping
    public ResponseVO addApply(@RequestBody Apply apply){
        if (apply==null){
            return new ResponseVO<>(null,"新增失败",4000);
        }
        int row = applyService.addApply(apply);
        if (row >0 ){
            return new ResponseVO<>(row,"新增成功",2000);
        }
        return new ResponseVO<>(null,"新增失败",4000);
    }

    /**
     * @description:  根据预算申请编号查询预算申请
     * @param: applyId 预算申请编号
     * @return: com.zh.web.vo.ResponseVO<com.zh.po.Apply>
     */
    @GetMapping("/{applyId}")
    public ResponseVO<Apply> findApplyById(@PathVariable Integer applyId){
        Apply apply = applyService.findApplyById(applyId);
        if (apply == null){
            return new ResponseVO<>(null,"查询失败",4000);
        }else {
            return new ResponseVO<>(apply, "查询成功", 2000);
        }
    }

    /**
     * @description:  用户修改申请
     * @param: apply   预算申请
     * @return: com.zh.web.vo.ResponseVO
     */
    @PutMapping("/update")
    public ResponseVO updateApply(@RequestBody Apply apply){
        if (apply==null){
            return new ResponseVO<>(null,"修改失败",4000);
        }

        int row = applyService.updateApply(apply);
        if (row >0 ){
            return new ResponseVO<>(row,"修改成功",2000);
        }
        return new ResponseVO<>(null,"修改失败",4000);
    }

    /**
     * @description: 用户还款
     * @param: apply 预算申请
     * @return: com.zh.web.vo.ResponseVO
     */
    @PutMapping("/backBudget")
    public ResponseVO backBudget(@RequestBody Apply apply){
        if(apply==null){
            return new ResponseVO<>(null,"还款失败",4000);
        }
            Integer applyUserId = apply.getApplyUser();
            User userByUserId = userService.getUserByUserId(applyUserId);
            if (userByUserId==null){
                return new ResponseVO<>(null,"还款失败",4000);
            }
            Double userMoney = userByUserId.getUserMoney();// 现有金额
            Double userBudget = userByUserId.getUserBudget();// 贷款总金额
            Double applyMoney = apply.getApplyMoney();// 贷款申请金额
            if (userMoney>=applyMoney){
                userByUserId.setUserMoney(userMoney-applyMoney);
                userByUserId.setUserBudget(userBudget-applyMoney);
                userService.updateUser(userByUserId);
                applyService.backBudget(apply);
                return new ResponseVO<>(null,"还款成功",2000);
            }
            return new ResponseVO<>(null,"还款失败",4000);
    }

}
